package ucn.dmf83.sc.workshop1.group3.UILayer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

import ucn.dmf83.sc.workshop1.group3.ControlLayer.AddressBookCtrl;
import ucn.dmf83.sc.workshop1.group3.DomainLayer.Copy;
import ucn.dmf83.sc.workshop1.group3.DomainLayer.DVD;

public class DisksMenu {
	
	private static AddressBookCtrl controller;
	
	private static void clear() {
		int i = 0;
		while(i < 25)
		{
			System.out.println("");
			i++;
		}
	}
	
	private static void showHeader(String menuName) {
		clear();
   	 	System.out.println("------------------------------------------------");
        System.out.println("-                                              -");
        System.out.println("-    AddressBook v0.1 by Group3 Inc.           -");
        System.out.println("-                                              -");
        System.out.println("------------------------------------------------");
        System.out.println("\n\n\f *** " + menuName + " ***\n\n");
	}
	
    public static void showMenu() {
    	showHeader("DVDs and Copies");
        int choice = printDVDMenu();
        
        switch(choice) {
        case 1:
            findDVD();
        case 2:
            addDVD();
        case 3:
        	printAllDVD();
        case 4:
       		MainMenu.showMenu();
        default:
            System.out.println("Invalid selection, please try again!" + choice);
            showMenu();
        }
   }
   
   public static void showMenu(AddressBookCtrl controller1)
   {
	   	controller = controller1;
	   	showMenu();
   }
   
   public static int printDVDMenu() {
       System.out.println(" 1. Search");
       System.out.println(" 2. Add DVD");
       System.out.println(" 3. List all DVD");
       System.out.println(" 4. Return");

       System.out.print("\n \n Please type in the number of your requested option: ");
       
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       String data = null;
		try {
			data = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
       
		int i = 0;
 		
 		try {
 			i = Integer.parseInt(data);
 		}
 		catch(Exception e) {
 			e.printStackTrace();
 		}
 		
        return i;
   }
   
   // TODO Add actual functionality, UI drawing, etc!
   
   public static void findDVD() {
	   showHeader("Search for DVD");
	   
	   System.out.println("Enter DVD title: ");
	   
	   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       String data = null;
		try {
			data = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		DVD dvd = controller.getDVD(data);
		
		System.out.println("DVD ID: " + dvd.getId());
		   System.out.println("DVD Title: " + dvd.getTitle());
		   System.out.println("Published: " + dvd.getPublishDate());
		   System.out.println("");
		   System.out.println("Actors: ");
		   for(Iterator<String> ita = dvd.getArtists().iterator(); ita.hasNext();) {
			   System.out.println("\t\f" + ita.next());
		   }
		   System.out.println("\n\n------------------------------------------------");
		   System.out.println("\t\t COPIES");
		   for(Iterator<Copy> itb = controller.getCopies(dvd).iterator(); itb.hasNext();) {
			   Copy copy = itb.next();
			   System.out.println("\n\nCopy Serial Number: " + copy.getSerialNum());
			   System.out.println("Purchased: " + copy.getPurchaseDate());
			   System.out.println("Price: " + copy.getPurchasePrice());
		   }
		   System.out.println("\n\n------------------------------------------------");
		   
		   System.out.println("\n\nEnter one of the following numbers to use the function:\n");
		   
		   System.out.println("1 - Add copy \n2 - Back to main menu\n\n");
		   
		   System.out.println("Enter publish date:");
		   data = null;
			try {
				data = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		switch(Integer.parseInt(data)) {
		case 1:
			addCopy(dvd.getId());
		case 2:
			showMenu();
		}
		   
   }
   
   public static void addDVD() {
	   showHeader("Add new DVD");
	   String data;
	   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	   
	   
	   System.out.println("Enter DVD Title:");
	   data = null;
		try {
			data = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		String title = data;
		
		System.out.println("Enter publish date:");
		   data = null;
			try {
				data = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			String publishDate = data;
			
	   
	   controller.addDVD(title, publishDate);
   }
   
   public static void addCopy(int dvdID) {
	   showHeader("Add new Copy to DVD #" + dvdID);
	   String data;
	   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	   
	   DVD dvd = controller.getDVD(dvdID);
	   
	   
	   System.out.println("Enter copy SerialNum:");
	   data = null;
		try {
			data = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		String serialNum = data;
	
		System.out.println("Enter purchase date:");
		   data = null;
			try {
				data = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		String purchaseDate = data;
		
		System.out.println("Enter purchase price:");
			data = null;
			try {
				data = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
	   
	   controller.addCopy(dvd, serialNum, purchaseDate, Double.parseDouble(data));
   }
   
   
   public static void printAllDVD() {
	   showHeader("DVD List");	   
	   
	   ArrayList<DVD> dvds = controller.getDVDs();
	   
	   if(dvds == null) {
		   System.out.println("No DVDs found");
		   showMenu();
	   }
	   for(Iterator<DVD> it = dvds.iterator(); it.hasNext();) {
		   DVD dvd = it.next();
		   System.out.println("DVD ID: " + dvd.getId());
		   System.out.println("DVD Title: " + dvd.getTitle());
		   System.out.println("Published: " + dvd.getPublishDate());
		   System.out.println("");
		   System.out.println("Actors: ");
		   for(Iterator<String> ita = dvd.getArtists().iterator(); ita.hasNext();) {
			   System.out.println("\t\f" + ita.next());
		   }
		   System.out.println("\n\n------------------------------------------------");
		   System.out.println("\t\t COPIES");
		   for(Iterator<Copy> itb = controller.getCopies(dvd).iterator(); itb.hasNext();) {
			   Copy copy = itb.next();
			   System.out.println("\n\nCopy Serial Number: " + copy.getSerialNum());
			   System.out.println("Purchased: " + copy.getPurchaseDate());
			   System.out.println("Price: " + copy.getPurchasePrice());
		   }
		   System.out.println("\n\n------------------------------------------------");
	   }
	   
	   Scanner sc = new Scanner(System.in);
	   System.out.println("Press Enter to return to AddressBook menu");
	   sc.nextLine();
	   showMenu();
	   sc.close();
   }
}
